#require 'net/http'
require 'mechanize'

@continue = nil
def get(dir, js)
p dir

page = Net::HTTP.get_response(URI.parse("http://www.3arabiamusic.com/singers/#{js}")).body.scan(/\/E.+.zip/)

page.each {|filename|
  p filename.scan(/\/0.+.zip/)[0].gsub('/', '')

  unless File.exist?(".\\#{dir}\\#{filename.scan(/\/0.+.zip/)[0].gsub('/', '')}")
    zip = Net::HTTP.get_response(URI.parse("http://www.3arabiamusic.com#{filename}")).body
    file = open(".\\#{dir}\\#{filename.scan(/\/0.+.zip/)[0].gsub('/', '')}","wb")
    file.write(zip)
    file.close
  end
}
end

WWW::Mechanize.new.get('http://www.3arabiamusic.com/singers/index.html').links.each do |link|
  singer = link.href.grep(/.html/)[0]
  if singer and singer.length>0
    dirname = singer.scan(/.+\//)[0].gsub('/', '')
     puts "Skipping #{dirname} ..." 
      next if 'rida_el_abdallah' != dirname
    begin
    FileUtils.mkdir(dirname)
    rescue
      puts "Skipping #{dirname} ..."
      next
    end
    puts "Processing #{dirname}"
    
    list06 = WWW::Mechanize.new.get("http://www.3arabiamusic.com/singers/#{singer}").body#.body.grep(/\/.+.js/)
      jss = list06.gsub("\\",'').gsub('.js',".js\n").scan(/SRC=.+.js/)
      jss.each {|js|
        if js.include?(dirname)
          js_file = js.scan(/\/|".+.js/)[0].gsub('/','').gsub('"','').gsub('..','')
          get(dirname,js_file)
        end
      }      
  end
end

#page = Net::HTTP.get_response(URI.parse('http://www.3arabiamusic.com/singers/kadim_al_sahir-entaha_almeshwar.js')).body
#open(".\\kadim_al_sahir\\kadim_al_sahir-entaha_almeshwar.js","wb").write(page)


#page = Net::HTTP.get_response(URI.parse('http://www.3arabiamusic.com/singers/kadim_al_sahir-entaha_almeshwar.js')).body
#open(".\\kadim_al_sahir\\kadim_al_sahir-entaha_almeshwar.js","wb").write(page)

